/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-18
 * Time: 9:36
 */
public class Sort {
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public static void shellSort(int[] array) {
        int gap = array.length / 2;
        while (gap >= 1) {
            gap /= 2;
            shell(array, gap);
        }
    }
    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for(; j < array.length; j -= gap) {
                if(array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;//记录最小值的下标
            for (int j = i + 1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            //走完这里，找到最小元素的下标minIndex
            //交换
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }
    //创建大根堆
    public static void createHeap(int[] array) {
        //从最后一个父节点开始向下调整，下标依次往前减
        //parent = (child - 1) / 2; 左：child = parent * 2 + 1 右：child = parent * 2 + 2
        for (int i = (array.length - 1 - 1) / 2; i >= 0 ; i--) {
            siftDown(array, i, array.length);
        }
    }
    //向下调整
    public static void siftDown(int[] array, int parent, int length) {
        //定义一个child为该父节点的左孩子
        int child = parent * 2 + 1;
        while (child < length) {
            //比较改父节点的左右孩子，把值最大的孩子作为交换节点
            if(array[child] < array[child + 1]) {
                child += 1;
            }
           //比较父节点和孩子节点大小
            if(array[parent] < array[child]) {
                //交换
                int tmp = array[parent];
                array[parent] = array[child];
                array[child] = tmp;
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }
    //堆排序
    public static void heapSort(int[] array) {
        //先转换成大根堆
        createHeap(array);
        //开始换，然后向下转换
        for (int i = array.length - 1; i > 0 ; i--) {
            //i下标的节点和堆顶交换
            int tmp = array[0];
            array[0] = array[i];
            array[i] = tmp;
            //向下调整
            siftDown(array,0, i);
        }
    }
    //冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;//标记
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]) {
                    //交换
                    int tmp =array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flag = true;
                }
            }
            if(!flag) {
                break;
            }
        }
    }
    public static void quickSort(int[] array) {
        int left = 0;
        int right = array.length - 1;
        quick(array, left, right);
    }
    public static void quick(int[] array, int start, int end) {
        //递归结束条件
        if(start >= end) {
            return;
        }
        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }
    public static int partition(int[] array, int left, int right) {
        //找到一个下标元素，左边都比这个下标元素小，右边都比这个下标元素大，并且还要返回这个下标
        //记录下标为0的值，放在tmp中
        int tmp = array[0];
        while (left < right) {
            //先走右边
            if(left < right && array[right] >= tmp) {
                right--;
            }
            if(left < right && array[left] <= tmp) {
                left++;
            }
            //左下标的值大于tmp，右下标的值小于tmp，这两个下标值交换
            int newTmp = array[left];
            array[left] = array[right];
            array[right] = newTmp;
        }
        //走到这，left和right相遇了,left下标的值和tmp交换,并且返回这个位置的下标
        int newTmp = tmp;
        tmp = array[left];
        array[left] = newTmp;
        return left;
    }
    public static void mergeSort(int[] array) {
        int start = 0;
        int end = array.length - 1;
        mergeSortFunc(array, start, end);
    }
    //套壳
    public static void mergeSortFunc(int[] array, int start, int end) {
        //递归结束标志
        if(start >= end) {
            return;
        }
        //求出中间节点位置
        int mid = (start + end) / 2;
        //左边
        mergeSortFunc(array, start, mid);
        //右边
        mergeSortFunc(array, mid + 1, end);
        //合并
        merge(array, start, mid, end);
    }

    //合并
    public static void merge(int[] array, int left, int mid, int right) {
        //定义mid两边的左右下标
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        //定义一个新的数组，存放array排序完后的数组
        int[] tmpArray = new int[right - left - 1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            //比较左右两边s1和s2的值
            if(array[s1] < array[s2]) {
                tmpArray[k++] = array[s1++];
            } else {
                tmpArray[k++] = array[s2++];
            }
            if(s1 <= e1) {
                tmpArray[k++] = array[s1++];
            }
            if(s2 <= e2) {
                tmpArray[k++] = array[s2++];
            }
        }
        //拷贝到原数组
        for (int i = 0; i < tmpArray.length; i++) {
            array[left + i] = tmpArray[i];
        }
    }
}
